package com.mall.controller.protal;

import com.mall.common.Const;
import com.mall.common.MySessionContext;
import com.mall.common.ResponseCode;
import com.mall.common.ServerResponse;
import com.mall.pojo.User;
import com.mall.service.IUserService;
import org.apache.commons.lang.builder.ReflectionToStringBuilder;
import org.apache.ibatis.javassist.tools.reflect.Reflection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

/**
 * Created by xiaoyao on 2017/8/14.
 */
@Controller
@RequestMapping("/user/")
public class UserController {

    @Autowired
    private IUserService iUserService;

    /**
     * 用户登录
     * @param user
     * @param session
     * @return
     */
    @RequestMapping(value="login.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> login(User user,HttpSession session){
        System.out.println("---------Controller中传递参数user=--------"+user);
        //传入请求的参数
        ServerResponse<User> response = iUserService.login(user);
        //如果登录成功，就把登录用户的信息存到session
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
            Const.sessionId  = session.getId();
            System.out.println("---------Controller中设置参数Const.sessionId =--------"+Const.sessionId );
        }
        return response;
    }

    /**
     * 退出登录
     * @return
     */
    @RequestMapping(value="logout.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> logout(){
        System.out.println("---------logout被执行了--------");
        System.out.println("---------Const.sessionId：--------"+Const.sessionId);
        //把登录信息从session中清除
        if(Const.sessionId != null){
            HttpSession session = MySessionContext.getSession(Const.sessionId );
            Const.sessionId = null;
            session.removeAttribute(Const.CURRENT_USER);
            System.out.println("--------Const.sessionId --------"+Const.sessionId );
            return ServerResponse.createBySuccess("已退出登录");
        }
        else{
            return ServerResponse.createBySuccess("没有找到登录信息！");
        }

    }

    /**
     * 用户注册
     * @param user
     * @return
     */
    @RequestMapping(value="register.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> register(User user){
        System.out.println("---------Controller中传递参数user=--------"+user);
        //把需注册的user对象传给注册方法；
        return iUserService.register(user);
    }

    /**
     * 数据校验
     * @param type
     * @param str
     * @return
     * 供前端调用，及时响应单条校验结果
     */
    @RequestMapping(value="checkValid.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkValid(String type, String str){
        return iUserService.checkValid(type, str);
    }

    /**
     * 获取已登录的用户信息
     * @return
     * 已登录的用户信息存在session中
     * 未登录的输出提示信息
     */
    @RequestMapping(value="getUserInfo.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getUserInfo(){
        System.out.println("---------getUserInfo被执行了--------");
        System.out.println("---------Const.sessionId：--------"+Const.sessionId);
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return ServerResponse.createBySuccess(user);
    }

    /**
     * 根据密保问题找回密码
     * 1、获取密保问题
     * 2、校验密保问题答案
     * 3、重置密码
     */
    @RequestMapping(value="checkUserQuestion.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkUserQuestion(String username){
        return iUserService.checkUserQuestion(username);
    }
    @RequestMapping(value="checkAnswer.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> checkAnswer(String username,String question,String answer){
        return iUserService.checkAnswer(username, question, answer);
    }
    @RequestMapping(value="forgetResetPassword.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> forgetResetPassword(String username,String passwordNew,String forgetToken){
        return iUserService.forgetResetPassword(username, passwordNew, forgetToken);
    }

    /**
     * 登录状态下的重置密码
     * @param passwordOld
     * @param passwordNew
     * @return
     * 查看session中是否有登录用户
     * 调用重置密码的方法
     */
    @RequestMapping(value="resetPassword.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<String> resetPassword(String passwordOld,String passwordNew){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        System.out.println("---------重置密码被执行了Const.sessionId =--------"+Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iUserService.resetPassword(user,passwordOld,passwordNew);
    }

    /**
     * 获取用户信息
     * @return
     * 从session中获取用户信息，
     * 如果没有登录则强制登录；
     */
    @RequestMapping(value="getInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> getInformation(){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User user = (User) session.getAttribute(Const.CURRENT_USER);
        return iUserService.getInformation(user.getId());
    }

    /**
     * 修改用户个人信息
     * @param user
     * @return
     * 1、判断是否登录；
     * 2、如果登录了，修改用户信息，其中userId不可更改；
     */
    @RequestMapping(value="updateInformation.do", method = RequestMethod.POST)
    @ResponseBody
    public ServerResponse<User> updateInformation(User user){
        //判断是否登录
        if(Const.sessionId == null){
            return ServerResponse.createByErrorCodeMessage(ResponseCode.NEED_LOGIN.getCode(),"用户未登录，请先登录");
        }
        HttpSession session = MySessionContext.getSession(Const.sessionId );
        User sessionUser = (User) session.getAttribute(Const.CURRENT_USER);
        //用户id不允许修改，还设置为原先的；
        user.setId(sessionUser.getId());
        ServerResponse<User> response = iUserService.updateInformation(user);
        if(response.isSuccess()){
            session.setAttribute(Const.CURRENT_USER,response.getData());
        }
        return response;
    }






}
